package org.dg.iag.query;

import java.util.Map;
import java.util.Map.Entry;

import org.dg.iag.util.ApplicationConstants;

public class XQueryUtil {

	public static String generateXpathCondition (Map<String,String> map, boolean includeBrackets ) {
		if (map != null && map.size() > 0 ) {
			String ret	= "";
			int count	= 0;
			for (Entry<String, String> entry : map.entrySet()) {
				if (count > 0) {
					ret	+= " and ";
				}
				String []values	= entry.getValue().split(ApplicationConstants.URL_API_OR_SEPARATOR);
				if ( values != null && values.length > 1 ) {
					String orCondition	= "";
					for ( int i=0; i< values.length; i++ ) {
						orCondition		+= entry.getKey() + "='" + values[i] + "'";
						if ( i!= values.length-1 )
							orCondition	+= " or ";
					}
					ret		+= "(" + orCondition + ")";
				}
				else
					ret		+= "(" + entry.getKey() + "='" + entry.getValue() + "')";
				count++;
			}
			if (includeBrackets) {
				ret 	= "[" + ret + "]";
			}
			return ret;
		}
		return null;
	}

}
